
# 终端编译指令
# make clean && clear && make






##################################################################
# 
# 以下信息是需要配置的
# 
##################################################################


SDK := /opt/apps/android/sdk
PLATFORM_VERSION := 34
MINI_SDK_VERSION := 28
BD_TOOLS_VERSION := android-13

# # See https://maven.google.com/web/index.html for list of libraries
# LIBS = androidx.core:core:1.2.0:aar \
# 	   androidx.activity:activity:1.1.0:aar \
# 	   androidx.fragment:fragment:1.2.3:aar \
# 	   androidx.appcompat:appcompat:1.1.0:aar \
# 	   androidx.appcompat:appcompat-resources:1.1.0:aar \
# 	   androidx.lifecycle:lifecycle-common:2.2.0:jar \
# 	   androidx.lifecycle:lifecycle-viewmodel:2.2.0:aar \
# 	   androidx.lifecycle:lifecycle-runtime:2.2.0:aar \
# 	   androidx.savedstate:savedstate:1.0.0:aar \
# 	   androidx.drawerlayout:drawerlayout:1.0.0:aar \
# 	   androidx.collection:collection:1.1.0:jar \
# 	   androidx.arch.core:core-common:2.1.0:jar \
# 	   androidx.vectordrawable:vectordrawable:1.1.0:aar \
# 	   androidx.customview:customview:1.0.0:aar

#LIBS =	androidx.core:core:1.2.0:aar \
#		androidx.appcompat:appcompat:1.1.0:aar \
#		androidx.appcompat:appcompat-resources:1.1.0:aar \
#		androidx.drawerlayout:drawerlayout:1.0.0:aar

# LIBS =

APP_XML_PATH = ./AndroidManifest.xml
PACKAGE_NAME:= $(strip $(subst package=,,$(subst ",,$(shell cat $(APP_XML_PATH)|grep package))))
SOURCE_DIR = ./java/target/com/tinyapp

KEYSTORE?= keystore.jks

# NOTE: DO NOT OVERRIDE THE PASSWORD HERE, instead set it as an enviroment
# variable
KEYSTORE_PASS?= "password"

LIBS_URL := https://maven.aliyun.com/repository/google/

java_sources =
java_sources+= $(SOURCE_DIR)/*.java
# java_sources+= $(SRC_DIR)/com/zzvm/MainActivity.java
# kotlin_files := $(shell find $(SOURCE_DIR) -type f -name '*.kt')
# java_sources := $(shell find $(SOURCE_DIR) -type f -name '*.java')


##################################################################
# 
# 以上信息是需要配置的
# 
##################################################################













HOST_CPU_CORES:=${shell getconf _NPROCESSORS_ONLN}
MAKEFLAGS:=-s # -j${HOST_CPU_CORES} #加-s参数表示 silence，即指令无回显

# Generated variables
PLATFORM = $(SDK)/platforms/android-$(PLATFORM_VERSION)
BUILD_TOOLS = $(SDK)/build-tools/$(BD_TOOLS_VERSION)
DIR_DOWNLOAD=3rdlibs
PACKAGE_PATH = $(subst .,/,$(PACKAGE_NAME))


lib_package = $(word 1, $(subst :, ,$1))
lib_name    = $(word 2, $(subst :, ,$1))
lib_version = $(word 3, $(subst :, ,$1))
lib_type    = $(word 4, $(subst :, ,$1))
lib_filename = $(call lib_name,$1)-$(call lib_version,$1).$(call lib_type,$1)

LIBS_NAME = $(foreach lib,$(LIBS),$(call lib_package,$(lib))/$(call lib_name,$(lib))-$(call lib_version,$(lib)))

LIBS_RAW = $(foreach lib,$(LIBS), $(subst .,/,$(call lib_package,$(lib)))/$(call lib_name,$(lib))/$(call lib_version,$(lib))/$(call lib_filename,$(lib)))
LIBS_DOWNLOAD = $(addprefix $(DIR_DOWNLOAD)/, $(LIBS_RAW))
LIBS_DOWNLOAD_AAR = $(filter %.aar, $(LIBS_DOWNLOAD))
LIBS_DOWNLOAD_JAR = $(filter %.jar, $(LIBS_DOWNLOAD))

LIBS_AAR_EXTRACTED = $(LIBS_DOWNLOAD_AAR:$(DIR_DOWNLOAD)/%.aar=build/libs/extracted/%)
LIBS_AAR_COMPILED  = $(LIBS_DOWNLOAD_AAR:$(DIR_DOWNLOAD)/%.aar=build/libs/compiled/%.zip)
LIBS_AAR_R_JAVA    = $(LIBS_DOWNLOAD_AAR:$(DIR_DOWNLOAD)/%.aar=build/libs-gen/%)
LIBS_AAR_R_JAVA_FILE  = $(foreach dir,$(LIBS_AAR_R_JAVA),$(shell find $(dir) -type f -name '*.java'))
LIBS_JAR_FILES = $(LIBS_DOWNLOAD_JAR) $(addsuffix /classes.jar, $(LIBS_AAR_EXTRACTED))

EMPTY :=
SPACE := $(EMPTY) $(EMPTY)
LIBS_CP_FLAGS = $(subst $(SPACE),:,$(LIBS_JAR_FILES))
LIBS_RES_FLAGS = $(addprefix -S , $(addsuffix /res, $(LIBS_AAR_EXTRACTED)))
LIBS_INCLUDE_FLAGS = $(addprefix -I , $(LIBS_DOWNLOAD))
LIBS_LINK_FLAGS = $(addprefix -R , $(LIBS_AAR_COMPILED))

all: apk
.PHONY: all

$(LIBS_DOWNLOAD):
	$(eval package=$(subst .,/,$(word 1, $(subst /, ,$(@:$(DIR_DOWNLOAD)/%.aar=%)))))
	$(eval name_version=$(word 2, $(subst /, ,$(@:$(DIR_DOWNLOAD)/%.aar=%))))
	$(eval version=$(lastword $(subst -, ,$(name_version))))
	$(eval name=$(name_version:%-$(version)=%))
	@mkdir -p $(shell dirname $@)

	@echo -e "\e[31m\n正在下载 $@\n　　网址 $(LIBS_URL)$(@:$(DIR_DOWNLOAD)/%=%)\e[0m"
	mkdir -p $(DIR_DOWNLOAD)
	[ -f "$@" ] || wget -q $(LIBS_URL)$(@:$(DIR_DOWNLOAD)/%=%) -P $(shell dirname $@)
	touch -c $@

$(LIBS_AAR_EXTRACTED): build/libs/extracted/%: $(DIR_DOWNLOAD)/%.aar
	@mkdir -p $@
	@mkdir -p $@/res

	@echo -e -n "\e[31m库解压中 \e[0m"
	unzip -oq $< -d $@
	@echo -e "ok\t$@"

$(LIBS_AAR_COMPILED):
	@mkdir -p $@
	$(BUILD_TOOLS)/aapt2 compile --dir $(@:build/libs/compiled/%.zip=build/libs/extracted/%) -o $@

$(LIBS_AAR_R_JAVA): build/libs-gen/%: build/libs/extracted/%
	@mkdir -p $@
	@touch -c $@
	@echo -e "\e[31m\n正在生成 库的 R.java ($@)\e[0m"
	$(BUILD_TOOLS)/aapt package -f -m -J $@ $(LIBS_RES_FLAGS) -M $</AndroidManifest.xml -I $(PLATFORM)/android.jar --auto-add-overlay

build/gen/$(PACKAGE_PATH)/R.java: $(LIBS_AAR_EXTRACTED)
	@mkdir -p build/gen/$(PACKAGE_PATH)
	@echo -e "\e[31m\n正在生成 app的R.java ($@)\e[0m"
	$(BUILD_TOOLS)/aapt package -f -m -J build/gen/ -S res $(LIBS_RES_FLAGS) -M $(APP_XML_PATH) -I $(PLATFORM)/android.jar --auto-add-overlay

preinfo:
	@echo -e "\e[31m\n正在编译 $(PACKAGE_NAME)\e[0m"

dl: $(LIBS_AAR_EXTRACTED)
	:

jar: build/gen/$(PACKAGE_PATH)/R.java $(java_sources) $(kotlin_files) $(LIBS_JAR_FILES) $(LIBS_AAR_R_JAVA)
	@mkdir -p build/classes
	@echo -e "\e[31m\n正在编译所有的 R.java\e[0m"
	javac -source 8 -target 8 -cp $(PLATFORM) -d build/classes build/gen/$(PACKAGE_PATH)/R.java $(LIBS_AAR_R_JAVA_FILE)
	@echo "ok"

ifneq ("$(java_sources)","")
	@echo -e "\e[31m\n正在编译java源码\e[0m"
	javac -source 8 -target 8 -cp $(PLATFORM)/android.jar:$(LIBS_CP_FLAGS):build/classes -d build/classes $(java_sources)
endif
ifneq ("$(kotlin_files)","")
	@echo "kotlin_files: $(kotlin_files)"
	@echo -e "\e[31m\n正在编译kt源码\e[0m"
	kotlinc                   -cp $(PLATFORM)/android.jar:$(LIBS_CP_FLAGS):build/classes -d build/classes $(kotlin_files)
endif

	touch -c build/classes

	@echo -e '\e[31m\nclass => jar\e[0m'
	jar cf build/app.jar -C build/classes/$(PACKAGE_PATH) .
	@echo "ok"



dex: jar $(LIBS_JAR_FILES)
	@mkdir -p build/apk

	@echo -e "\e[31m\njar   => class.dex\e[0m"
	"$(BUILD_TOOLS)/d8" --output  tmp.zip --min-api $(MINI_SDK_VERSION) build/app.jar $(LIBS_JAR_FILES)
	unzip -oq tmp.zip
	rm -rf tmp.zip
	mv -f classes.dex build/apk/classes.dex
	@echo "ok"

$(KEYSTORE):
	@echo -e "\e[31m\n正在生成公私密钥对($(KEYSTORE))\e[0m"
	@echo -e "\e[31m(此密钥对用来对apk进行签名，故也称为签名证书。此证书是您的电子签名笔，不可以外传!)\e[0m"
	@echo -e "\e[31m请填写您的个人信息，这些信息将被存入证书中\e[0m"
	keytool -genkey -alias example -keyalg RSA -keystore $(KEYSTORE)

apk: preinfo $(KEYSTORE) dex $(LIBS_AAR_EXTRACTED) $(LIBS_DOWNLOAD)

	@echo -e "\e[31m\n正在生成 build/app.apk\e[0m"
	$(BUILD_TOOLS)/aapt package --auto-add-overlay -S res $(LIBS_RES_FLAGS) -f -m -M $(APP_XML_PATH) -I $(PLATFORM)/android.jar $(LIBS_INCLUDE_FLAGS) -F build/app.apk build/apk/
	@echo "ok"

	@echo -e "\e[31m\n正在签名 build/app.apk\e[0m"
	$(BUILD_TOOLS)/apksigner sign --ks $(KEYSTORE) --ks-pass "pass:$(KEYSTORE_PASS)" build/app.apk	
	@echo "ok"

install: apk
	adb install -r build/app.apk
.PHONY: install

run: install
	adb shell monkey -p $(PACKAGE_NAME) 1
.PHONY: run

clean:
	rm -rf build
.PHONY: clean
